package com.example.springboot.service.impl;

import com.example.springboot.domain.Collect;
import com.example.springboot.domain.User;
import com.example.springboot.mapper.CollectMapper;
import com.example.springboot.service.CollectService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    CollectMapper collectMapper;

    //    插入收藏
    @Override
    public void set(Collect collect) {
        User user = TokenUtils.getCurrentUser();
        collect.setUserId(user.getUserid());
        Collect dblCollect = collectMapper.selectUserCollect(collect);
        if (dblCollect == null) {
            collectMapper.insert(collect);
        } else {
            collectMapper.deleteById(dblCollect.getId());
        }
    }

//    查询当前用户是否收藏过
    @Override
    public Collect selectUserCollect(Integer fid, String module) {
        User user = TokenUtils.getCurrentUser();
        Collect collect = new Collect();
        collect.setUserId(user.getUserid());
        collect.setFid(fid);
        collect.setModule(module);
        return collectMapper.selectUserCollect(collect);
    }

    //    查询该博客收藏量
    @Override
    public int selectByFidAndModule(Integer fid, String module) {
        return collectMapper.selectByFidAndModule(fid, module);
    }

    @Override
    public void deleteById(Integer id) {
        collectMapper.deleteById(id);
    }
}
